<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="../frame/header.jsp"></jsp:include>

<title>学习小组管理</title>
<style>
div.in-line{
	margin-top:5px;
}
div.in-line label{
	width:100px;
	text-align:right;
}

</style>
</head>

<body>

	<div class="container-fluid">
		<div id="market" class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">学术讨论</a> <span class="divider">/</span></li>
					<li><a href="#">学习小组</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-th"></i> 学习小组列表
					</h2>

					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<div id="search-form">
						<form id="searchForm" method="post" class="form-search span12" action="<c:url value="/admin/study/search_group"/>">
							<div class="in-line">
							<label>院系及专业：</label>
							<select class="input-xlarge" data-placeholder="全部" id="sorts_0" name="sortId" data-rel="chosen">
								<option value="0">全部</option>
								
							</select>
							</div>
							<div class="in-line">
							<label>小组名称：</label>
							<input type="hidden" name="pageNum" id="pageNum" value="${searchForm.pageNum }">
							<input type="hidden" name="infoId" id="infoId" value="${searchForm.infoId }">
							<input type="text" name="search" id="searchText" value="${searchForm.search }" 
								class="search-query input-xlarge" placeholder="请输入学习小组名称">
							<button type="button" id="searchBtn" class="btn btn-success search-btn">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
							<button type="button" id="newBtn" class="btn btn-danger pull-right">创建学习小组</button>
							</div>
						</form>
						
					</div>
					<table id="infos" class="table table-striped table-bordered ">
						<thead>
							<tr>
								<th>序号</th>
								<th>院系专业</th>
								<th>组名</th>
								<th>创建时间</th>
								<th>创始人</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach var="group" items="${pageModel.result }" varStatus="status">
								<tr>
									<td>${status.index+1 }</td>
									<td>${group.sortName }</td>
									<td>${group.name } </td>
									<td>${group.dateline }</td>
									<td>${group.founderName }</td>
									
									<td><a href="javascript:edit('${group.groupId }')"><i
											class="icon-edit"></i></a> &nbsp;&nbsp; <a
										href="javascript:del('${group.groupId }')"><i class="icon-minus"></i></a></td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					
					<div id="mypager" class="mypager"></div>
				</div>
			</div>
		</div>
		<!--/fluid-row-->

	</div>
	<!--/.fluid-container-->

	<jsp:include page="../frame/scripts.jsp"></jsp:include>
	<script type="text/javascript">
	var pageNum = "${pageModel.pageNum}";
	var totalPage = "${pageModel.totalPage}";
	var numPerPage = "${pageModel.numPerPage}";
	
	$(document).ready(function(){
		
		$("#newBtn").click(function(){
			//跳转至新增页面
			window.location.href = '<c:url value="/admin/study/addGroup" />';
		})
		
		$('#mypager').bootstrapPaginator({
			currentPage:pageNum,
			totalPages:totalPage,
			numberOfPages:numPerPage,
			onPageClicked:function(event, originalEvent, type,page){
				$("input[name='pageNum']").val(page);
				$("#searchForm").submit();
			}
		});
		
		
		$('#searchBtn').click(function(){
			$("#searchForm").submit();
		});
		
		//初始化
		$.ajax({
			url:'<c:url value="/admin/study/faculty/tree" />',
			dataType:'json',
			success:function(data){
				//搜索表单初始化版块下拉框
				createTree(data,"sorts");
				//设置选中
				$("#sorts_0").val("${searchForm.sortId}");
				
				//增加下拉框样式
				//$('[data-rel="chosen"]').chosen();
			}
		})
		
	})
	
	function edit(infoId){
		window.location.href = '<c:url value="/admin/study/editGroup?groupId='+infoId+'"/>';
	}
	
	function del(infoId){
		if(confirm("确定要删除吗？")){
			$("input[name='infoId']").val(infoId);
			$("#searchForm").submit();
		}
	}
	
	function createTree(data,tagId){
		
		$.each(data,function(i,item){
			//生成节点
			createNode(item,tagId);
			if(item.children.length > 0){
				//生成子节点
				createTree(item.children,tagId);
			}
		})
	}
	
	function createNode(item,tagId){
		
		var $parent_select = $("#"+tagId+"_"+item.sortParent);
		var str = '';
		if(item.children.length > 0){
			str += '<optgroup id="'+tagId+'_'+item.sortId+'" label="'+item.sortName+'">';
			str += '</optgroup>';
		}else{
			str += '<option value='+item.sortId+'>'+item.sortName+'</option>';
		}
		$parent_select.append(str);
	}
	</script>
</body>
</html>
